<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>用户维护</title>
  </head>
  <body>
    <script type="text/javascript">
      var newUserWin;
      var editUserWin;
      var userDS;
      Ext.onReady(function(){
        var userId;
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
        //------------------------------业主Grid--------------------------------
        userDS = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: '/user/getAllUsers'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'name'
            },{
              name: 'password'
            },{
              name: 'sub_type'
            },{
              name: 'is_locked'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        //house的CM
        var userCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),{
            id: 'common',
            header: '数据库编号',
            dataIndex: 'id',
            width: 30,
            hidden:true
          },{
            header: '用户名',
            dataIndex: 'name',
            width: 70
          },{
            header: '密码',
            dataIndex: 'password',
            width: 80
          },{
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderUserIsLucked,
            width: 80
          },{
            header: '类型',
            dataIndex: 'sub_type',
            renderer: renderUserSubType,
            width: 100
          },{
            header: '生成日期',
            dataIndex: 'created_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 110
          },{
            header: '修改日期',
            dataIndex: 'updated_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 110
          }
        ]);
        //render 用户是否已经锁定
        function renderUserIsLucked(value, cellmeta, record, rowIndex, columnIndex, store){
          if (record.get("is_locked")=="1"){
            return "<font color=red>锁定<font>";
          }else{
            return "<font color=green>未锁定<font>";
          }
        }
        
        //render用户类型
        function renderUserSubType(value, cellmeta, record, rowIndex, columnIndex, store){
          if (record.get("sub_type")=="admin"){
            return "<font color=red>管理员<font>";
          }else{
            return "<font color=green>员工<font>";
          }
        }
        
        //生成要订单的产品的Grid
        var userGrid = new Ext.grid.GridPanel({
          store: userDS,
          cm: userCM,
          renderTo: 'user_search',
          //width: 830,
          height: screenHeight-260,
          loadMask: true,
          //frame: true,
          collapsible: true,
          //autoExpandColumn: 'common',
          title: '用户列表',
          frame: false
        });

        userGrid.on('rowcontextmenu', function(housegrid, rowIndex, e){
          userId = userDS.getAt(rowIndex).get('id');
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var addUserMenuItem = new Ext.menu.Item({
          text: '【添加新用户】',
          iconCls: 'addItem',
          handler: rightMenuNewUserFn
        });
        var editUserMenuItem = new Ext.menu.Item({
          text: '【编辑用户】',
          iconCls: 'editItem',
          handler: rightMenuEditUserFn
        });
        
        var userLockMenuItem = new Ext.menu.Item({
          text: '【锁定与解锁】',
          //iconCls: 'album',
          handler: rightMenuLockUserFn
        });

        function rightMenuNewUserFn(grid, rowIndex, e){
          newUserWin = new Ext.Window({
            layout: 'fit',
            width: 210,
            height: 130,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            title: '添加新用户',
            autoLoad: {
              url: "/user/newUser",
              scripts: true
            }
          });
          newUserWin.resizable = false;
          newUserWin.show();
        }
        function rightMenuEditUserFn(grid, rowIndex, e){
          editUserWin = new Ext.Window({
            layout: 'fit',
            width: 210,
            height: 130,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            title: '编辑用户',
            autoLoad: {
              url: "/user/editUser?user_id="+userId,
              scripts: true
            }
          });
          editUserWin.resizable = false;
          editUserWin.show();
        }
        function rightMenuLockUserFn(grid, rowIndex, e){
          Ext.Ajax.request({
            url : '/user/userLock',
            success : function (result, request) {
              userDS.reload();
            },
            failure : function (result, request){
              Ext.MessageBox.alert("提示","通讯失败，请从新操作");
            },
            method : 'POST',
            params : {
              user_id : userId,
              authenticity_token: authToken.getValue()
            }
          });
        }
        
        var rightClick = new Ext.menu.Menu({
          items: [addUserMenuItem,editUserMenuItem,userLockMenuItem]
        });
        
        userDS.load();
      })
    </script>
    <div id="user_search"></div>
  </body>
</html>
